package cn.rui.controller;

import cn.rui.constant.MessageConstant;
import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.entity.Result;
import cn.rui.pojo.Permission;
import cn.rui.service.PermissionService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限管理
 *
 * @author 徽州大都督
 * @date 2020/9/3
 */
@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;


    /**
     * 分页查询所有权限
     *
     * @param
     * @param queryPageBean
     * @return
     */
    @PostMapping("findAllPmsByRole")
    public List<Permission> findAllPmsByRole() {


        return permissionService.findPageByRoleId ();
    }

    /**
     * 带搜索条件分页查询
     */
    @PostMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return permissionService.findPage (queryPageBean);
    }

    /**
     * 添加权限
     */
    @PostMapping("handleAdd")
    public Result handleAdd(@RequestBody Permission permission) {
        try {
            permissionService.handleAdd (permission);
            return new Result (true, MessageConstant.ADD_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (true, MessageConstant.ADD_PERMISSION_FAIL);
        }

    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("handleUpdate/{id}")
    public Permission handleUpdate(@PathVariable("id")Integer id){
        return permissionService.findById(id);
    }

    /**
     * 编辑
     * @param permission
     * @return
     */
    @PostMapping("handleEdit")
    public Result handleEdit(@RequestBody Permission permission){
        try {
            permissionService.handleEdit(permission);
            return new Result (true,MessageConstant.EDIT_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.EDIT_PERMISSION_FAIL);
        }
    }

    /**
     * 根据id删除
     */
    @GetMapping("handleDelete/{id}")
    public Result handleDelete(@PathVariable("id")Integer id){
        try {
            permissionService.handleDelete(id);
            return new Result (true,MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace ();
            return new Result (false,MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }
}
